package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.entity.CampaignBonus;
import com.ddyx.entity.CampaignInstance;
import com.ddyx.entity.CampaignTask;
import com.ddyx.mapper.CampaignBonusMapper;
import com.ddyx.mapper.CampaignInstanceMapper;
import com.ddyx.mapper.CampaignTaskMapper;
import com.ddyx.service.ICampaignTaskService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CampaignTaskServiceImpl implements ICampaignTaskService {
	@Resource
	CampaignTaskMapper campaignTaskMapper;

	@Resource
	CampaignInstanceMapper campaignInstanceMapper;

	@Resource
	CampaignBonusMapper campaignBonusMapper;

	@Override
	public ResponseForm insertCampaignTask(RequestForm param) {
		ResponseForm result = new ResponseForm();
		CampaignTask campaignTask = new CampaignTask();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(campaignTask, paramMap);

			CampaignInstance campaignInstance = campaignInstanceMapper.selectByPrimaryKey(campaignTask.getInstanceId());

			// 判断红包是否已拆
			if (campaignInstance.getBonusStatus() == 0) {
				campaignTaskMapper.insertSelective(campaignTask);// 成功加入

				// --------检测参团是否满人
				CampaignTask campaignTask2 = new CampaignTask();
				campaignTask2.setInstanceId(campaignTask.getInstanceId());
				List<CampaignTask> list = campaignTaskMapper.select(campaignTask2);
				// 判断list的size 是否 为4
				Date date = new Date();
				if (list.size() == 4) {
					// 手动设置---固定值--后期更改
					for (CampaignTask c : list) {
						CampaignBonus bonus = new CampaignBonus();
						bonus.setCampId(campaignInstance.getInstanceId());
						bonus.setInstanceId(campaignInstance.getInstanceId());
						bonus.setWxUid(c.getWxUid());
						bonus.setBonusAmount(10.00);
						bonus.setIssueDatetime(date);
						bonus.setValidDate(date);
						bonus.setInvalidStatus(0);
						bonus.setApplyStatus(0);
						bonus.setUsedStatus(0);
						bonus.setNickName(c.getNickName());
						bonus.setHeadImage(c.getHeadImage());
						campaignBonusMapper.insertSelective(bonus);
					}
					result.setData(list);
					result.setMessage("成功抢到红包10.00元,请到 个人中心-我的红包 查看!");
					result.setCode("1");
					// 更改开团表 红包状态
					campaignInstance.setBonusStatus(1);
					campaignInstanceMapper.updateByPrimaryKeySelective(campaignInstance);
				} else {
					result.setData(list);
					result.setMessage("成功加入,等待其余的" + (4 - list.size()) + "位朋友加入!!!");
				}
			} else {
				result.setStatus(false);
				result.setMessage("很遗憾,来晚一步,红包已经被拆分...");
			}

		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

}
